<?php

namespace LhpAmis\Renderers;

/**
 * ServiceEach 列表行字段以标签方式展示多条数据。 参考文档：https://aisuda.bce.baidu.com/amis/zh-CN/components/each
 *
 * @author  slowlyo
 * @version 6.7.0
 */
class ServiceEach extends Service
{
    protected $each = null;
    protected $eachs = [];

    public function __construct()
    {
        $this->set('type', 'service');
        if ($this->each == null) {
            $this->each = Each::make();
            $this->each->set("source", '${items}');
        }
    }

    public function eachs($variable)
    {
        foreach ($variable as $name => $value) {
            $this->eachs[$name] = $value;
        }
        $this->body($this->eachs);
        return $this;
    }
    public function each($name, $value)
    {

        if ($name !== '') {
            $this->each->set($name, $value);
        }

        $this->body($this->each);
        return $this;
    }
    /**
     * 描述标题
     */
    public function name($value = '')
    {
        return $this->set('name', $value);
    }

    /**
     * 描述标题
     */
    public function label($value = '')
    {
        return $this->set('label', $value);
    }

    /**
     * CURD构建器字段初始化
     * @return void
     */
    public function tableBuildField($item)
    {
        if (isset($item[0]['items'])) {

            $this->body($this->each->items($item[0]['items']));
            unset($item[0]['items']);
        }
        $this->extendsAttributes($item, 4);
        return $this;
    }
}
